package com.hncc.kumao.tool;

import com.hncc.kumao.biz.IOfflineDownloadTaskBiz;
import com.hncc.kumao.entity.NetdiskAccessToken;
import com.hncc.kumao.entity.OfflineDownloadTask;
import com.hncc.kumao.entity.OfflineDownloadTaskInfo;
import com.hncc.kumao.util.MyDownloadProgress;
import com.hncc.kumao.util.OfflineDownloadUtils;

public class DownloadFileTool {
    
    private IOfflineDownloadTaskBiz offlineDownloadTaskBiz;
    public void setOfflineDownloadTaskBiz(IOfflineDownloadTaskBiz offlineDownloadTaskBiz) {
        this.offlineDownloadTaskBiz = offlineDownloadTaskBiz;
    }
    
    //下载百度网盘文件
    public void downloadBaiDuFile(OfflineDownloadTask offlineDownloadTask, 
            NetdiskAccessToken netdiskAccessToken) throws Exception {
        try {
            System.out.println("下载百度网盘上的文件");
            BaiDuOperation.download(offlineDownloadTask, netdiskAccessToken, 
                    new MyDownloadProgressImpl(offlineDownloadTask, netdiskAccessToken));
        } catch (Exception e) {
            //e.printStackTrace();
            throw e;
        }
    }
    
    //下载坚果云文件
    public void downloadJianGuoYunFile(OfflineDownloadTask offlineDownloadTask, 
            NetdiskAccessToken netdiskAccessToken) throws Exception {
        try {
            System.out.println("下载坚果云上的文件");
            JianGuoYunOperation.download(offlineDownloadTask, netdiskAccessToken, 
                    new MyDownloadProgressImpl(offlineDownloadTask, netdiskAccessToken));
        } catch (Exception e) {
            //e.printStackTrace();
            throw e;
        }
    }
    
    //下载FTP文件
    public void downloadFTPFile(OfflineDownloadTask offlineDownloadTask, 
            NetdiskAccessToken netdiskAccessToken) throws Exception {
        try {
            System.out.println("下载FTP上的文件");
            FTPOperation.download(offlineDownloadTask, netdiskAccessToken, 
                    new MyDownloadProgressImpl(offlineDownloadTask, netdiskAccessToken));
        } catch (Exception e) {
            //e.printStackTrace();
            throw e;
        }
    }
    
    //下载Seafile文件
    public void downloadSeafileFile(final OfflineDownloadTask offlineDownloadTask, 
            NetdiskAccessToken netdiskAccessToken) throws Exception {
        try {
            System.out.println("下载Seafile上的文件");
            SeafileOperation.download(offlineDownloadTask, netdiskAccessToken, 
                    new MyDownloadProgressImpl(offlineDownloadTask, netdiskAccessToken));
        } catch (Exception e) {
            //e.printStackTrace();
            throw e;
        }
    }
    
    //下载SMB文件
    public void downloadSMBFile(OfflineDownloadTask offlineDownloadTask, 
            NetdiskAccessToken netdiskAccessToken) throws Exception {
        try {
            System.out.println("下载SMB上的文件");
            SMBOperation.download(offlineDownloadTask, netdiskAccessToken, 
                    new MyDownloadProgressImpl(offlineDownloadTask, netdiskAccessToken));
        } catch (Exception e) {
            //e.printStackTrace();
            throw e;
        }
    }
    
    //下载进度的实现类
    public class MyDownloadProgressImpl implements MyDownloadProgress {
        
        private OfflineDownloadTask offlineDownloadTask;
        private NetdiskAccessToken netdiskAccessToken;
        private final boolean[] flag = {true};
        
        public MyDownloadProgressImpl(OfflineDownloadTask offlineDownloadTask, 
                NetdiskAccessToken netdiskAccessToken) {
            this.offlineDownloadTask = offlineDownloadTask;
            this.netdiskAccessToken = netdiskAccessToken;
        }
        
        @Override
        public void onProgress(Long currentSize, Long totalSize) {
            try {
                //从redis中获取数据
                OfflineDownloadTaskInfo taskInfo = OfflineDownloadUtils
                        .getTaskProgressRate(offlineDownloadTask.getId());
                if (flag[0]) {
                    // 第一次回显进度
                    flag[0] = false;
                    // 获取到总大小并更新数据库
                    System.out.println("第一次回显进度");
                    offlineDownloadTask.setFileSize(totalSize + "");
                    offlineDownloadTaskBiz.update(offlineDownloadTask);
                    taskInfo.setTotalSize(totalSize + "");
                }
                // 更新当前进度
                taskInfo.setCurrentSize(currentSize + "");
                // 更新redis
                OfflineDownloadUtils.setTaskProgressRate(taskInfo);
            } catch (Exception e) {
                //e.printStackTrace();
                throw e;
            }
        }
    }
}
